package net.avh4.scratch.rpg;

import java.io.IOException;
import java.util.Random;

import net.avh4.math.QuadTreeArray;

public abstract class CachedWorldMap extends WorldMap {
	
	protected Random rand = new Random();
	
	private QuadTreeArray<Integer> cache = new QuadTreeArray<Integer>();
	private TerrainType terrain[] = { land, water };
	
	public CachedWorldMap() throws IOException {
		super();
	}
	
	protected int getTile(int x, int y) {
		Integer value = cache.get(x, y);
		if (value == null) return -1;
		return value.intValue();
	}
	
	protected void setTile(int x, int y, int tile) {
		assert tile >= 0;
		assert tile < terrain.length;
		cache.set(x, y, tile);
	}

	@Override
	public TerrainType getTerrain(int x, int y) {
		Integer type = getTile(x, y);
		if (type == -1) {
			return blank;
		} else {
			assert type < terrain.length;
			return terrain[type];
		}
	}
	

}
